<template>
  <div class="product-table">
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="笔记本" name="bijiben">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="bijibenming" label="商品名称"></el-table-column>
          <el-table-column prop="pinpai" label="品牌"></el-table-column>
          <el-table-column prop="jiage" label="价格"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="scope">
              <el-image 
                style="width: 50px; height: 50px"
                :src="scope.row.tupian"
                :preview-src-list="[scope.row.tupian]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
              <el-button size="mini" type="primary" @click="handleReview(scope.row)">审核</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="电脑主机" name="diannaozhuji">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="zhujiming" label="商品名称"></el-table-column>
          <el-table-column prop="pinpai" label="品牌"></el-table-column>
          <el-table-column prop="jiage" label="价格"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="scope">
              <el-image 
                style="width: 50px; height: 50px"
                :src="scope.row.tupian"
                :preview-src-list="[scope.row.tupian]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
              <el-button size="mini" type="primary" @click="handleReview(scope.row)">审核</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="硬件组装" name="yingjianzuzhuang">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="yingjianming" label="商品名称"></el-table-column>
          <el-table-column prop="yingjiantype" label="硬件类型"></el-table-column>
          <el-table-column prop="pinpai" label="品牌"></el-table-column>
          <el-table-column prop="jiage" label="价格"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="scope">
              <el-image 
                style="width: 50px; height: 50px"
                :src="scope.row.tupian"
                :preview-src-list="[scope.row.tupian]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
              <el-button size="mini" type="primary" @click="handleReview(scope.row)">审核</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="电脑外设" name="diannaowaishe">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="waishemingcheng" label="商品名称"></el-table-column>
          <el-table-column prop="waishefenlei" label="外设分类"></el-table-column>
          <el-table-column prop="pinpai" label="品牌"></el-table-column>
          <el-table-column prop="jiage" label="价格"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="scope">
              <el-image 
                style="width: 50px; height: 50px"
                :src="scope.row.tupian"
                :preview-src-list="[scope.row.tupian]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleDetail(scope.row)">详情</el-button>
              <el-button size="mini" type="primary" @click="handleReview(scope.row)">审核</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>

    <!-- 商品详情对话框 -->
    <el-dialog :title="detailTitle" :visible.sync="detailVisible" width="800px">
      <product-detail :product="currentProduct"></product-detail>
    </el-dialog>
  </div>
</template>

<script>
import ProductDetail from './ProductDetail'

export default {
  components: {
    ProductDetail
  },
  props: {
    status: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      activeTab: 'bijiben',
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      detailVisible: false,
      currentProduct: null
    }
  },
  computed: {
    detailTitle() {
      return `商品详情: ${this.currentProduct?.name || ''}`
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleTabClick() {
      this.page = 1
      this.getList()
    },
    getList() {
      const params = {
        page: this.page,
        limit: this.limit,
        status: this.status
      }
      this.$http.get(`${this.activeTab}/review/list`, {params}).then(({data}) => {
        if (data && data.code === 0) {
          this.tableData = data.data.list
          this.total = data.data.total
        }
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    handleDetail(row) {
      this.currentProduct = {
        ...row,
        table: this.activeTab
      }
      this.detailVisible = true
    },
    handleReview(row) {
      this.$emit('review', {
        ...row,
        table: this.activeTab
      })
    }
  }
}
</script>

<style scoped>
.product-table {
  margin-top: 20px;
}
</style>